* Figures Figure 4 (left panel), Figure 6 (right panel), Figure 7 (right panel), Figure 8 (right panel),
* Figure A1 (left panel), Figure A3 (a) (left panel), Figure A3 (b) (left Panel)

use "temp/EIA_clean.dta", clear

egen sector_year = group(year sector)

gen i94 = 0
replace i94=1 if year==1994

foreach x of var mi si xi {
gen `x'94b = 0
replace `x'94b = . if i94==1 & `x'==.
replace `x'94b = `x' * i94
bys id: egen `x'94 = total(`x'94b)
replace `x'94=. if `x'94b==. & `x'94==0
drop `x'94b
}

gen deltam = mi - mi94
gen growthm = deltam / mi94 * 100
gen deltas = si - si94
gen deltae = xi- xi94
gen lmi = log(mi)
gen lmi94 = log(mi94)
gen deltamlog = lmi - lmi94
gen growthsd = ((1-si)/(1-si94)-1)*100
gen growthsi = (si/si94-1)*100
gen growthsx = (xi/xi94-1)*100
gen lsi = log(si)
gen lsi94 = log(si94)
gen lxi = log(xi)
gen lxi94 = log(xi94)
gen deltals = lsi-lsi94
gen deltale = lxi - lxi94
local graphtitle "Mexico"
local graphtitle_quant "Mexican Data"

*2. Figure 4 (left panel):
binscatter growthm si94 if year>=1995 & year<=1999& si94>0 & si94!=. & growthm<=1000, absorb(sector_year) n(80) xtitle(Initial Import Share) ytitle(Market Share Growth) title("{bf: `graphtitle'}", color(black)) graphregion(color(white))
graph export "../../Output/F4_A.pdf", replace

* Figure 6 (right panel):
binscatter growthm xi94 if year>=1995 & year<=1999& xi94>0 & xi94!=. & growthm<=1000, absorb(sector_year) n(80) xtitle(Initial Export Share) ytitle(Market Share Growth) title("{bf: `graphtitle'}", color(black)) graphregion(color(white))
graph export "../../Output/F6_B.pdf", replace

* Figures 7 and 8 (right panels):
binscatter growthm si94 if year>=1995 & year<=1999& si94>0 & si94!=. & growthm<=1000, absorb(sector_year) n(80) xtitle(Initial Import Share) ytitle(Market Share Growth) title("{bf: `graphtitle_quant'}", color(black)) graphregion(color(white))
graph export "../../Output/F7_and_8_B.pdf", replace

* Figure A1 (left panel):
binscatter xi si, absorb(sector_year) n(80) xtitle(Import Share) ytitle(Export Share) graphregion(color(white))  title("{bf: `graphtitle'}", color(black)) graphregion(color(white))
graph export "../../Output/FA1_A.pdf", replace

* Figure A3(a) (left panel)
binscatter deltas deltae if year>=1995 & year<=1999 & deltae!=0 & deltae!=.& deltas!=0 & deltas!=., absorb(sector_year) n(80) xtitle(Change Export Share) ytitle(Change Import Share) title("{bf: `graphtitle'}", color(black)) graphregion(color(white))
graph export "../../Output/FA3_a_1.pdf", replace

* Figure A3(b) (left panel)
binscatter deltamlog deltae if year>=1995 & year<=1999 & deltae!=0 & deltae!=.& deltamlog!=0 & deltamlog!=., absorb(sector_year) n(80) xtitle(Change Export Share) ytitle(Change Log Market Share) title("{bf: `graphtitle'}", color(black)) graphregion(color(white))
graph export "../../Output/FA3_b_1.pdf", replace
